/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 @file
 This is a hash table implementation for 32 bit numbers and strings. These functions
 are thread safe.
 */
#ifndef _hashtable_h
#define _hashtable_h

#ifdef __cplusplus
extern "C" {
#endif


#include "crcore.h"

/**
 The hashtable type set at initialization.
 */
typedef enum
{
	HASHTABLE_UINT32=0,/*!< Use Thomas Wang's 32 bit Hash Function. */
	HASHTABLE_STRING=1 /*!< Use the ELF string hash function. Can be used for any data type. */
} hash_table_type_t;

struct bucket_node_t;
typedef struct bucket_node_t bucket_node_t;

/**
 The hashtable
 */
typedef struct
{
	uint32_t bucket_count; /*!< The number of buckets. Do not modify after hashtable_init().*/
	uint32_t nodes_added; /*!< Number of nodes inserted. */
	list_t* buckets; /*!< list of buckets (buckets are lists) */
	pthread_mutex_t mutex; /*!< mutex for the table. */
	uint32_t mask; /*!< mask used after hashing. */
	uint32_t(*hash_func)(uint32_t mask, void* key, uint32_t keylen);
	int(*key_compare)(bucket_node_t* a, void* key, uint32_t keylen);
	void(*key_assign)(bucket_node_t* node, void* key, uint32_t keylen);
	void(*key_cleanup)(bucket_node_t* node);
} hashtable_t;

/**
 Initialize a hash table of either HASHTABLE_UINT32 type or HASHTABLE_STRING type. returns 0 on success.

 It is possible to use the HASHTABLE_STRING type for any data type, for example 64 bit integers.
 The UINT32 type uses a 32 bit hash function. The STRING type uses the ELF symbol name hashing function.
 @param h The hashtable structure to initialize.
 @param type HASHTABLE_UINT32 or HASHTABLE_STRING.
 @param bucket_count Number of hash buckets. Must be a power of 2 (1,2,4,8,16,...)
*/
int hashtable_init( hashtable_t* h, hash_table_type_t type, uint32_t bucket_count );

/**
 Cleanup a hash table, the callback is called for every entry

 When items are inserted into the table, often there is no reference for them, except in the hashtable, so
 during the cleanup, the callback function will be called for every item in the list (when the value is
 not NULL).
 @param h The table.
 @param callback The cleanup function for elements in the table.
 */
void hashtable_cleanup( hashtable_t* h, void(*callback)(void*) );

/**
 Insert a value into the hashtable. returns 0 on success, -1 on fail (if the item is already in the table).
 
 @param h The table.
 @param key A pointer to the key.
 @param keylen The length of the key in bytes.
 @param value The value to insert in the hash table
 */
int hashtable_insert( hashtable_t* h, void* key, uint32_t keylen, void* value );

/**
 Delete a value from the hashtable. returns 0 on success, -1 on fail (if the item wasn't already in the table).
 
 @param h The table.
 @param key A pointer to the key.
 @param keylen The length of the key in bytes.
 @param value The value associated with the key.
 */
int hashtable_delete( hashtable_t* h, void* key, uint32_t keylen, void** value );

/**
 Lookup a value in the hashtable. returns 0 on success, -1 on fail (if the item is not in the table).
 
 @param h The table.
 @param key A pointer to the key.
 @param keylen The length of the key in bytes.
 @param value The value retrieved in the hash table
 */
int hashtable_lookup( hashtable_t* h, void* key, uint32_t keylen, void** value );

/*!
 Return the number of items in the hashtable.
 @param h the table
 */
int hashtable_count( hashtable_t* h );

#ifdef __cplusplus
}
#endif


#endif
